package com.example.kotlinExample.controller;

/**
 * @author liliang
 */
public class Combination {

    static int count = 0;

    static void DFS(int[] arr, int layer, int size, int selectCount, int[] result, int startIndexInThisLayer) {

        int i = 0, k = 0;
        if (layer == selectCount) {

            for (i = startIndexInThisLayer; i < size; i++) {
                for (k = 0; k < layer - 1; k++) {
                    System.out.print(result[k] + " ");
                }
                System.out.println(arr[i]);
                count++;
            }

            return;
        }
        for (i = startIndexInThisLayer; i < size - selectCount + layer; i++) {
            result[layer - 1] = arr[i];
            DFS(arr, layer + 1, size, selectCount, result, i + 1);
        }
    }

    static void start(int[] arr, int selectCount) {
        if (selectCount < 1 || selectCount > arr.length) {
            return;
        }
        int[] result = new int[arr.length];
        // arr原数据数组  1固定值    arr.length数据个数   selectCount选择数量   result结果集  0 固定值
        DFS(arr, 1, arr.length, selectCount, result, 0);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

        int i;
        //测试
        for (i = 1; i < 10; i++) {
            System.out.println(arr.length + " 选 " + i);
            count = 0;
            start(arr, i);
            System.out.println("======共" + count + "种组合======\n");
        }
    }

}
